/*
*
* Copyright (c) 2011 Vaulting Systems International
* 
* Permission is hereby granted, free of charge, to any person obtaining a copy 
* of this software and associated documentation files (the "Software"), to deal 
* in the Software without restriction, including without limitation the rights 
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies 
* of the Software, and to permit persons to whom the Software is furnished to do  
* so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in all  
* copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, 
* INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A 
* PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT 
* HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION 
* OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE  
* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
*
*/
package com.ekeyman.ws.controller;

import javax.inject.Inject;
import javax.inject.Named;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import com.ekeyman.ws.service.EkeymanService;
import com.ekeymanlib.dto.AppDeviceApiKeys;
import com.ekeymanlib.dto.AppDeviceRequest;
import com.ekeymanlib.dto.EncryptionKeyRequest;
import com.ekeymanlib.dto.EncryptionKeys;
import com.ekeymanlib.dto.VendorProfile;

@Controller
public class EkeymanEndpoint {
	
    private static final Log logger = LogFactory.getLog(EkeymanEndpoint.class);
    
	@Inject
	@Named("ekeymanService")
	private EkeymanService ekeymanService;
	
	@RequestMapping(method=RequestMethod.POST, value="/create", headers="Accept=application/json")
    public @ResponseBody EncryptionKeys 
    			createEncryptionKeys(@RequestBody EncryptionKeyRequest request) {
    	
		if (logger.isDebugEnabled()) {
            logger.debug("Received CreateEncryptionKeysRequest for resource:'" + request.getResourceUri());
    	} 
		
    	EncryptionKeys encryptionKeys =  ekeymanService.createEncryptionKeys(
    			   request.getResourceUri(), 
    			   request.getPublicKey(), 
    			   request.getKeyBytesLength(), 
    			   request.getIvBytesLength(), 
    			   request.getSaltBytesLength(), 
    			   request.getIterationCountMax(),
    	    	   request.getLocation(), 
    	    	   request.getDigitalSignature());
    	    
     	        
    	        return encryptionKeys;
    }

	@RequestMapping(method=RequestMethod.POST, value="/get", headers="Accept=application/json")
    public @ResponseBody EncryptionKeys 
    			getEncryptionKeys(@RequestBody EncryptionKeyRequest request)  {
    	
		if (logger.isDebugEnabled()) {
            logger.debug("Received GetEncryptionKeysRequest for resource:'" + request.getResourceUri());
    	}
        
    	EncryptionKeys encryptionKeys =  ekeymanService.getEncryptionKeys(
		   request.getResourceUri(), 
		   request.getPublicKey(), 
    	   request.getLocation(), 
    	   request.getDigitalSignature());
    
           
        return encryptionKeys;
    }
    
	@RequestMapping(method=RequestMethod.POST, value="/delete", headers="Accept=application/json")
    public @ResponseBody boolean 
    			deleteEncryptionKeys(@RequestBody EncryptionKeyRequest request) {

		if (logger.isDebugEnabled()) {
            logger.debug("Received GetEncryptionKeysRequest for resource:'" + request.getResourceUri());
    	}
        
    	ekeymanService.deleteEncryptionKeys(
		   request.getResourceUri(), 
		   request.getPublicKey(), 
    	   request.getLocation(), 
    	   request.getDigitalSignature());
       
        return true;
    }
    
	@RequestMapping(method=RequestMethod.POST, value="/register", headers="Accept=application/json")
    public @ResponseBody  AppDeviceApiKeys 
    			registerAppDevice(@RequestBody AppDeviceRequest request) {
    	
		if (logger.isDebugEnabled()) {
            logger.debug("Received RegisterAppDeviceRequest for resource:'" + request.getApplication() + " " + request.getDevice());
    	}
        
    	AppDeviceApiKeys appDeviceApiKeys = ekeymanService.registerAppDevice(
    			request.getApiKey(), request.getApplication(), request.getDevice());
    
        return appDeviceApiKeys;
    }
}
